<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<%@ page import="com.tpix.PixieCore" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<!-- testing out lightbox to see if it works -->
	<script type="text/javascript" src="js/prototype.js"></script>
	<script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
	<script type="text/javascript" src="js/lightbox.js"></script>
	<link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
	<!--  the script above are for lightbox   viewer      -->
	

	<!-- get GET data for tag and album requests -->
	<%
		String tagSelection = request.getParameter("tag"); 		// eg. /home.jsp?tag=some+tag
		String albumSelection = request.getParameter("album"); 	// eg. /home.jsp?album=some+album
	%>
	
	<!-- Get references to Java objects -->
	<%  PixieCore core = (PixieCore) request.getSession().getAttribute("core");
		if (core == null) {
			core = new PixieCore();
			request.getSession().setAttribute("core", core);
		}
		// get photo URL iterator and page title, depending on GET parameters
		List<String> urls;
		List<Key> keys;
		String pageTitle;

		if (albumSelection != null) {
			// first look for album name in URL
			keys = core.getAlbumKeys(albumSelection);
			pageTitle = "Current Album:" + albumSelection;
		}else if (tagSelection != null) {
			// next look for tag in URL
			keys = core.getTagKeys(tagSelection);
			pageTitle = "Current Tag:" + tagSelection;
		}
		else {
			// if no GET parameter for tag or album, get current album from core
			keys = core.getAlbumKeys();
			pageTitle = "Current Album:" + core.getAlbum();
		}
		// get iterator for list of keys
		Iterator<Key>keysItr = keys.iterator();
		// name of currently selected album
		String currentAlbum = core.getAlbum();
		// for getting list of all user's albums
		ArrayList<String> albums = core.getAlbums();
		Iterator<String> albumItr = albums.iterator();
		// for getting list of all user's tags
		ArrayList<String> tags = core.getTags();
		Iterator<String> tagsItr = tags.iterator();
		%>
	<%-- Detect User Logged In --%>
	<%  if (!core.loggedIn()) {
			// user is not logged in. Redirect to login page
			response.sendRedirect("/login.jsp");
		}
		%>
	
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>Pixie - Album View</title>
	
	<link type="text/css" rel="stylesheet" href="/stylesheets/home.css" />
	<link type="text/css" rel="stylesheet" href="/stylesheets/960.css" />
	
	<!-- 	TypeKit -->
	<script type="text/javascript" src="http://use.typekit.com/waa6dbj.js"></script>
	<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
	
		
</head>
<body>
	<div class="container_12">
	<div id="wrapper">
	<br>
		
	<!-- Tags -->
	<div style="
	text-align:center;
	position:relative;
	top:0px; "> 
	<% 
		while (tagsItr.hasNext()) {
			String tag = (String) tagsItr.next();
			String tagURL = "/home.jsp?tag=" + tag.replace(' ','+');
			out.println("<a class=\"tag\" href=\"" + tagURL + "\">" + tag + "</a>");
		}
	%>
	</div> <!-- /tags --> <br>

	<!-- Album Navigation -->		
	<FORM action="homeservlet" method="post">
		<div class="grid_5">
			Albums:		
			<select name="album">
			<% 
				while (albumItr.hasNext()) {
					String selected = "";
					String album = (String) albumItr.next();
					//String currentAlbum = core.getAlbum(); // gets the name of the currently selected album
					if (album.equals(currentAlbum)) {selected = " selected=\"selected\"";}
					out.println("<option value=\"" + album + "\"" + selected + ">" + album + "</option>");
				}
			%>
			</select> 
			<input	type="submit" name="action" value="Choose Album">
			<input type="submit" name="action" value="Photo Info">
			
		</div>
		<div class="grid_2">
			<input	type="image" src="/images/upload_button.png" name="action" value="Upload a Photo">
		</div>
	  <!-- </FORM>  /album navigation -->

	
	
	<!-- Photo Details form 
	<FORM action="hometodetailsservlet" method="post">
	<div style="
	text-align:left;
	position:relative;
	top:0px;
	left:15px; "> 	-->
	
	
	<!-- </div> -->
	<!-- Photos -->
	<div class="clear"></div>
	<h1 align="center"><% out.println(pageTitle);%></h1><br>
	<%
		for(int i=0;i<6;i++){
			if(keysItr.hasNext()){
				Key tempKey = keysItr.next();
				String tempURL = core.URLfromKey(tempKey);
				%>
			<div class="grid_3">	
			<!--  added rel="lightbox[album]" here for lightbox slideshow viewer -->
			<% Integer value = new Integer(620); %>
			<center><a title="&lt;a href='<%=tempURL %>'> Click here to view full size &lt;/a>" rel="lightbox[album]" href="<%=tempURL+"=s"+value.toString()%>"><img src=<%=tempURL+"=s220" %> /></a></center>
			<center><input type="radio" name="whichphoto" value="<%= String.valueOf(tempKey.getId()) %>"></center> 
			</div>
		<% 	}
		}
	%> <!-- /photos -->
	
	</FORM>	<!-- /photo details form -->

	</div> <!-- /wrapper -->
	</div> <!-- /container-12 -->
</body>
</html>